package com.itboye.ihomebank.activity.keytwo.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.key.ActivityKaiSuoJiLu;
import com.itboye.ihomebank.activity.keytwo.KeyShouQuanActivity;
import com.itboye.ihomebank.adapter.AdminKeyAdapter;
import com.itboye.ihomebank.adapter.YongHuGuanLiAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.AdminKeyBean;
import com.itboye.ihomebank.bean.KeyOfLockBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SIDBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.KeyTwoPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 白马锁-锁钥匙列表
 */
public class AdminKeyFragment extends BaseFragment implements Observer{
    XListView key_listview;
    AdminKeyAdapter adapter;
    AdminKeyBean bean;
    private List<AdminKeyBean.ListKey> beans;
    KeyTwoPresenter presenter;
    private String uid;
    private String lockId;
    private String sid;

    @Override
    public int initView() {
        return R.layout.activity_guan_li_key;
    }
    @Override
    public void onResume() {
        super.onResume();
        beans = new ArrayList<>();
        if ("".equals(sid)){
            presenter.onKeyOfLock(uid, lockId);

        }else {
            presenter.onKeyOfLock(sid, lockId);

        }
    }
    @Override
    public void onMyActivityCreated() {
        key_listview.setPullRefreshEnable(false);
        presenter = new KeyTwoPresenter(this);

        sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
        uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
        lockId = getActivity().getIntent().getStringExtra("lockid");

    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getEventType() == KeyTwoPresenter.keyOfLock_success) {
                bean = (AdminKeyBean) entity.getData();
                if (beans != null) {
                    beans.addAll(bean.getList());
                    adapter = new AdminKeyAdapter(getActivity(), beans,
                            R.layout.item_yonghu_guanli);
                    key_listview.setAdapter(adapter);

                    key_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            // TODO Auto-generated method stub

                            try {
                                if (beans.get(position - 1).getType().equals("0")){//管理员不能进入自己的钥匙
                                    ByAlert.alert("管理员钥匙");
                                    return;
                                }
                                Intent intent = new Intent(getActivity(),
                                        KeyShouQuanActivity.class);
                                intent.putExtra("lockid", beans.get(position - 1).getLock_id());
                                intent.putExtra("touid", beans.get(position - 1).getUid());
                                intent.putExtra("keyid", beans.get(position - 1).getKey_id());
                                intent.putExtra("status", beans.get(position - 1).getStatus());
                                intent.putExtra("user_type", beans.get(position - 1).getType());
                                intent.putExtra("user_type", beans.get(position - 1).getType());
                                intent.putExtra("start", beans.get(position - 1).getStart());
                                intent.putExtra("end", beans.get(position - 1).getEnd());
                                startActivity(intent);
                            }catch (Exception e){

                            }

                        }
                    });
                }
            } else if (entity.getEventType() == KeyTwoPresenter.keyOfLock_fail) {
                if (entity.getData() instanceof ServerError ||entity.getData() instanceof TimeoutError){//服务器失败
                    //切换 锁备用服务器
                    showAlert();
                }else {
                    ByAlert.alert(entity.getData());
                }
            }else if (entity.getEventType()== UserPresenter.By_GETSID_success){//获取sid 成功
                SIDBean data1 = (SIDBean) entity.getData();
                SPUtils.put(MyApplcation.ctx,null, SPContants.LOCK_SESSION,data1.getSid());
                SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);
                presenter.onKeyOfLock(data1.getSid(), lockId);
            }else if (entity.getEventType()==UserPresenter.By_GETSID_fail){
                ByAlert.alert(entity.getMsg());
            }
        }
    }
}
